//
//  ChatPrescriptionCell.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/3/1.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol ChatPrescriptionCellDelegate: NSObjectProtocol {
    func didTappedPay(_ cell: ChatPrescriptionCell)
}

class ChatPrescriptionCell: UITableViewCell {
    
    weak var delegate: ChatPrescriptionCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }
    
    private var viewModel: ChatMsgViewModel?
    
    func configure(with vm: ChatMsgViewModel) {
        self.viewModel = vm
        sendTimeLabel.text = vm.sendTime
        recommandLabel.text = "医生意见：\(vm.prescriptionDetailVm.recommend)"
        timeLabel.text = vm.prescriptionDetailVm.passTime
        payBtn.isHidden = vm.prescriptionDetailVm.preStatus != .waitPay
        emptyLabel.isHidden = !vm.prescriptionDetailVm.showEmpty
        tableView.reloadData()
    }
    
    @IBOutlet private weak var sendTimeLabel: UILabel!
    
    @IBOutlet private weak var containerView: UIView! {
        didSet {
            containerView.layer.cornerRadius = 16
            containerView.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.isUserInteractionEnabled = false
            tableView.delegate = self
            tableView.dataSource = self
            tableView.rowHeight = 44
            tableView.estimatedRowHeight = UITableView.automaticDimension
            tableView.backgroundColor = .white
            tableView.tableFooterView = UIView()
            tableView.register(ChatPrescriptionRowCell.nibForCell(),
                               forCellReuseIdentifier: ChatPrescriptionRowCell.reuseIdentifier())
            tableView.register(ChatPrescriptionSectionHeader.nibForCell(),
                               forHeaderFooterViewReuseIdentifier: ChatPrescriptionSectionHeader.reuseIdentifier())
        }
    }
    
    @IBOutlet private weak var timeLabel: UILabel!
    
    @IBOutlet private weak var recommandLabel: UILabel! {
        didSet {
            recommandLabel.textColor = .textDark
            recommandLabel.font = .regular_14
        }
    }
    
    @IBOutlet weak var payBtn: UIButton! {
        didSet {
            payBtn.layer.cornerRadius = payBtn.height / 2
            payBtn.layer.masksToBounds = true
            payBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
            payBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
        }
    }
    
    @IBOutlet private weak var emptyLabel: UILabel! {
        didSet {
            emptyLabel.textColor = .textLight
            emptyLabel.font = .regular_14
        }
    }
}

extension ChatPrescriptionCell {
    @IBAction func payDrugCostAction(_ sender: UIButton) {
        delegate?.didTappedPay(self)
    }
}

extension ChatPrescriptionCell: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel?.prescriptionDetailVm.numberOfRows ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ChatPrescriptionRowCell.reuseIdentifier(),
                                                 for: indexPath) as! ChatPrescriptionRowCell
        if let vm = viewModel?.prescriptionDetailVm.viewModel(for: indexPath) {
            cell.configure(with: vm)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: ChatPrescriptionSectionHeader.reuseIdentifier()) as? ChatPrescriptionSectionHeader
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
}
